package br.ufrj.dcc.trabxml.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import br.ufrj.dcc.trabxml.util.TrabUtil;

/**
 * 
 * @author Artur
 */
public class FormBook extends JFrame {

    private boolean confirmed = false;
    
    private boolean cancelled = false;

    /** Creates new form FormBook */
    public FormBook() {
        initComponents();
        setLocationRelativeTo(null);
    }
    
    public void setVisible(boolean v) {
        pack();
        setSize(getWidth(),getHeight()+10);
        setLocationRelativeTo(null);        
        super.setVisible(true);
    }

    public void setBook(String isbn, String owner, String title,
            List<String> authors, String publisher, String obs) {
        lblISBN.setText(isbn);
        lblOwner.setText(owner);
        txtTitle.setText(title);
        txtPublisher.setText(publisher);
        txtObs.setText(obs);
        String strAuthors = null;
        if (authors.size() > 0) {
            for (String a : authors) {
                if (strAuthors == null) {
                    strAuthors = a;
                } else  {
                    strAuthors += "; " + a;
                }
            }
        }
        txtAuthors.setText(strAuthors);
        lblImage.setIcon(new ImageIcon(TrabUtil.getBookImage(getISBN())));
    }

    public String getISBN() {
        return lblISBN.getText();
    }

    public String getBookOwner() {
        return lblOwner.getText();
    }

    public String getBookTitle() {
        return txtTitle.getText();
    }

    public List<String> getAuthors() {
        String[] authorsArray = txtAuthors.getText().split(";");
        List<String> authors = new ArrayList<String>();
        for (int i = 0; i < authorsArray.length; i++) {
        	String a = authorsArray[i].trim();
        	if (a.length() > 0)
        		authors.add(a);
        }
        return authors;
    }

    public String getPublisher() {
        return txtPublisher.getText();
    }

    public String getObs() {
        return txtObs.getText();
    }    
    
    private ActionListener createBookListener = null;
    
    public void setActionListener(ActionListener listener) {
        createBookListener = listener;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        lblISBN = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblOwner = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtAuthors = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtObs = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtPublisher = new javax.swing.JTextField();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Livro");
        setResizable(false);
        jLabel1.setText("ISBN:");

        lblISBN.setText("um iSBN");

        jLabel3.setText("Dono:");

        lblOwner.setText("um dono");

        jLabel5.setText("T\u00edtulo:");

        txtTitle.setText("Meu titulo");

        jLabel6.setText("Autor(es):*");

        txtAuthors.setText("UM, dois, Tres");

        jLabel7.setText("Observa\u00e7\u00f5es:");

        txtObs.setColumns(20);
        txtObs.setRows(5);
        jScrollPane1.setViewportView(txtObs);

        jLabel8.setText("* Separe os autores por \";\" (ponto-e-v\u00edrgula)");

        jLabel9.setText("Editora:");

        txtPublisher.setText("uma editora");

        btnOK.setText("OK");

        btnCancel.setText("Cancelar");
        
        JPanel pnBookWithImage = new JPanel();
        pnBookWithImage.setLayout(new BorderLayout());
        lblImage = new JLabel();
        lblImage.setVerticalAlignment(JLabel.TOP);
        lblImage.setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 1));
        pnBookWithImage.add(lblImage,BorderLayout.WEST);
        JPanel pnContent = new JPanel();
        pnBookWithImage.add(pnContent,BorderLayout.CENTER);
        setContentPane(pnBookWithImage);        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                pnContent);
        pnContent.setLayout(layout);
        layout
                .setHorizontalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addContainerGap()
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addGroup(
                                                                                                                layout
                                                                                                                        .createParallelGroup(
                                                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                jLabel6)
                                                                                                                        .addComponent(
                                                                                                                                jLabel5)
                                                                                                                        .addComponent(
                                                                                                                                jLabel3)
                                                                                                                        .addComponent(
                                                                                                                                jLabel1))
                                                                                                        .addGap(
                                                                                                                17,
                                                                                                                17,
                                                                                                                17)
                                                                                                        .addGroup(
                                                                                                                layout
                                                                                                                        .createParallelGroup(
                                                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                lblISBN)
                                                                                                                        .addComponent(
                                                                                                                                lblOwner)
                                                                                                                        .addComponent(
                                                                                                                                txtTitle,
                                                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                307,
                                                                                                                                Short.MAX_VALUE)
                                                                                                                        .addComponent(
                                                                                                                                txtAuthors,
                                                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                307,
                                                                                                                                Short.MAX_VALUE)))
                                                                                        .addComponent(
                                                                                                jLabel8)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addGroup(
                                                                                                                layout
                                                                                                                        .createParallelGroup(
                                                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                jLabel7)
                                                                                                                        .addComponent(
                                                                                                                                jLabel9))
                                                                                                        .addGap(
                                                                                                                6,
                                                                                                                6,
                                                                                                                6)
                                                                                                        .addGroup(
                                                                                                                layout
                                                                                                                        .createParallelGroup(
                                                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                txtPublisher,
                                                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                307,
                                                                                                                                Short.MAX_VALUE)
                                                                                                                        .addComponent(
                                                                                                                                jScrollPane1,
                                                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                307,
                                                                                                                                Short.MAX_VALUE)))))
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGap(
                                                                                109,
                                                                                109,
                                                                                109)
                                                                        .addComponent(
                                                                                btnOK)
                                                                        .addGap(
                                                                                29,
                                                                                29,
                                                                                29)
                                                                        .addComponent(
                                                                                btnCancel)))
                                        .addContainerGap()));

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
                new java.awt.Component[] { btnCancel, btnOK });

        layout
                .setVerticalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel1)
                                                        .addComponent(lblISBN))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel3)
                                                        .addComponent(lblOwner))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel5)
                                                        .addComponent(
                                                                txtTitle,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel6)
                                                        .addComponent(
                                                                txtAuthors,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel9)
                                                        .addComponent(
                                                                txtPublisher,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(
                                                                jScrollPane1,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel7))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(btnOK)
                                                        .addComponent(btnCancel))
                                        .addContainerGap(13, Short.MAX_VALUE)));

        final JFrame form = this;

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setCancelled(true);
                form.dispose();
                createBookListener.actionPerformed(e);
            }

        });

        btnOK.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
            	if (!validateBook()) {
            		return;
            	}
                setConfirmed(true);
                form.dispose();
                createBookListener.actionPerformed(e);
            }

        });    
        setAlwaysOnTop(true);
        pack();
    }// </editor-fold>
    
    private boolean validateBook() {
    	if ((getBookTitle() == null) || (getBookTitle().length() < 1)) {
    		JOptionPane.showMessageDialog(this, "T\u00edtulo \u00e9 um campo obrigat\u00f4rio.", "Erro",
    				JOptionPane.ERROR_MESSAGE);
    		return false;
    	}
    	if (getAuthors().size() == 0) {
    		JOptionPane.showMessageDialog(this, "Autor(es) \u00e9 um campo obrigat\u00f4rio.", "Erro",
    				JOptionPane.ERROR_MESSAGE);
    		return false;
    	}     	
    	if ((getPublisher() == null) || (getPublisher().length() < 1)) {
    		JOptionPane.showMessageDialog(this, "Editora \u00e9 um campo obrigat\u00f4rio.", "Erro",
    				JOptionPane.ERROR_MESSAGE);
    		return false;
    	}    	
    	return true;
    }

    public void setCancelled(boolean cancelled) {
        this.cancelled = cancelled;
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public void setConfirmed(boolean confirmed) {
        this.confirmed = confirmed;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;

    private javax.swing.JButton btnOK;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLabel jLabel3;

    private javax.swing.JLabel jLabel5;

    private javax.swing.JLabel jLabel6;

    private javax.swing.JLabel jLabel7;

    private javax.swing.JLabel jLabel8;

    private javax.swing.JLabel jLabel9;

    private javax.swing.JScrollPane jScrollPane1;
    
    private JLabel lblImage;

    private javax.swing.JLabel lblISBN;

    private javax.swing.JLabel lblOwner;

    private javax.swing.JTextField txtAuthors;

    private javax.swing.JTextArea txtObs;

    private javax.swing.JTextField txtPublisher;

    private javax.swing.JTextField txtTitle;
    // End of variables declaration

}
